/*
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

*/

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include "print.h"
using namespace std;


void testForStack()
{
	stack<int> mystack;
	mystack.push(10);
	mystack.push(20);
	mystack.top() -= 5;
	cout << "mystack.top() is now " << mystack.top() << endl;
}

void testForIntToString()
{
	int a = 10;
	stringstream ss;
	ss << a;
	string str = ss.str();
	cout << str << endl;

	string str1 = to_string(a);

}



/**/
//Definition for singly-linked list.
struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };

class Solution {
public:
	ListNode *deleteDuplicates(ListNode *head) {
		ListNode *result = new ListNode(-1);

		ListNode *p = new ListNode(-1);
		ListNode *q = new ListNode(-1);


		if (head ==NULL)
		{
			return head;
		}

		//result->next = head;
		p = head;
		q = head->next;
		while (q != NULL)
		{
			if (q->val != p->val)
			{
				p = q;
				q = q->next;
			}
			else
			{
				p->next = q->next;
				q = q->next;

			}
		}
		

		return head;
	}
};


void printNode(ListNode *head)
{
	ListNode *p;
	p = head;

	cout << "The List is" << endl;
	while (p != NULL)
	{
		cout << p->val << endl;
		p = p->next;
	}
}



int main(int argc, char* argv[])
{

	Solution s;
	
	ListNode *head = new ListNode(0);

	ListNode *currtNode, *preNode;
	preNode = head;
	int a;

	for (int i = 1; i < argc; i++){

		ListNode *currtNode = new ListNode(0);
		cout << argv[i] << endl;
		a = atoi(argv[i]);
		currtNode->val = a;
		preNode->next = currtNode;
		preNode = currtNode;

	}

	head = head->next;
	preNode = NULL;

	printNode(head);

	cout << endl;
	cout << "Afer remover the Nth The list is:" << endl;
	head = s.deleteDuplicates(head);
	printNode(head);
	cout << endl;
	


	system("pause");
	return 0;
}